package com.hyperzsoft.budget;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyperzsoft.budget.DBProvider.BudgetTable;
import com.hyperzsoft.budget.DBProvider.ExpenseType;
import com.hyperzsoft.utils.ColorSelectBar;

public class TypeEditDialog {
	static private String TAG = "ExpenseList";
	
	private ArrayList<ExpenseType> mTypes = null;
	private typeListAdapter mAdapter;
	private DBProvider mDBProvider;
	private int mTypeOf;
	public Activity mParent;
	public Dialog mDialog;
	
	public void onCreate(int typeOf, Dialog dialog) {
		mTypeOf = typeOf;
		mDialog = dialog;
		mParent = mDialog.getOwnerActivity();
		
		mDBProvider = new DBProvider(mDialog.getContext());
		
		setupList();
		setupClickListeners();
	}
	
	public void setupClickListeners() {
		// Set Click Listener
		Button save =(Button)mDialog.findViewById(R.id.Edit_TypeSave); 
		save.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				saveTypeItem();
			}
		});
	}
	
	private void saveTypeItem() {
		
		mDBProvider.open();
		ExpenseType type = mDBProvider.new ExpenseType();
		
		type.NAME = ((EditText)mDialog.findViewById(R.id.Edit_TypeName)).getText().toString();
		type.COLOR = ((ColorSelectBar)mDialog.findViewById(R.id.Edit_TypeColor)).getColor();
		type.TYPE_OF = mTypeOf;
		
		if (mAdapter.mSelectedPos == -1) {
			type.insert();
			((RelativeLayout)mDialog.findViewById(R.id.types_list)).invalidate();
		} else {
			type._ID = mAdapter.getItemId(0);
			type.update();
		}
		
		mDBProvider.close();
		
		((EditText)mDialog.findViewById(R.id.Edit_TypeName)).setText("");
		((ColorSelectBar)mDialog.findViewById(R.id.Edit_TypeColor)).setColor();
		//setupList();
		mDialog.dismiss();
	}
	
	private void setupList() {
		ListView lv = (ListView)mDialog.findViewById(R.id.types_list_lv);
		
		// Convert the dps to pixels
		DisplayMetrics metrics = new DisplayMetrics();
		mParent.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		
		getTypesList();
		this.mAdapter = new typeListAdapter(mDialog.getContext(), mTypes);
		lv.setAdapter(mAdapter);
		
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mAdapter.setSelectedPosition(position);
				//TODO: Load the types values into the footer fields for edit
				// 	and enable the save/cancel buttons
			}
		});
	}
	
	private void getTypesList(){
		ArrayList<Object> workingList;
		
		try{
			mTypes = new ArrayList<ExpenseType>();
			mDBProvider.open();
			workingList = mDBProvider.selectAll(BudgetTable.EXPENSE_TYPE, mDialog.getContext().getString(R.string.expense_type_name) + " asc", "type_of = ?", new String[] {Integer.toString(mTypeOf)});
			mDBProvider.close();
			
			for (Object obj : workingList){
				mTypes.add((ExpenseType)obj);
			}
			
			workingList = null;
			
			//Thread.sleep(500);
			Log.i("ARRAY", ""+ mTypes.size());
		} catch (Exception e) {
			Log.e(TAG, e.getStackTrace().toString());
		}
		
		//runOnUiThread(returnRes);
	}
	
	public class typeListAdapter extends BaseAdapter {
		private final Context mContext;
		private ArrayList<ExpenseType> mItems = new ArrayList<ExpenseType>();
		private int mSelectedPos = -1;
		
		public typeListAdapter(Context context, ArrayList<ExpenseType> items) {
			mContext = context;
			mItems = items;
		}
		
		@Override
		public int getCount() {
			return mItems.size();
		}
		
		@Override
		public Object getItem(int position) {
			return mItems.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return (position == 0) ? 
					((mSelectedPos != -1) ? 
							mItems.get(mSelectedPos)._ID : 
							mSelectedPos) 
					: mItems.get(position)._ID;
		}
		
		public void setSelectedPosition(int pos){
			LinearLayout ll;
			mSelectedPos = pos;
			
			ExpenseType expenseTypes = mItems.get(mSelectedPos);
			ll = (LinearLayout)mDialog.findViewById(R.id.EditGroup_TypeName);
			((EditText)ll.findViewById(R.id.Edit_TypeName)).setText(expenseTypes.NAME);
			ll = (LinearLayout)mDialog.findViewById(R.id.EditGroup_TypeColor);
			((ColorSelectBar)ll.findViewById(R.id.Edit_TypeColor)).setColor(expenseTypes.COLOR);
			
			// inform the view of this change
			notifyDataSetChanged();
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater)mParent.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.types_list_item, null);
			}
			
			LinearLayout ll = (LinearLayout)v.findViewById(R.id.type_list_item);
			
			if(mSelectedPos == position){
				v.setBackgroundColor(Color.parseColor(mContext.getString(R.color.list_item_royal_blue)));
				//list_item_corn_yellow
				((TextView)v.findViewById(R.id.type_name)).setTextColor(Color.parseColor(mContext.getString(R.color.list_item_corn_yellow)));
				ll.setBackgroundColor(Color.parseColor(mContext.getString(R.color.list_item_translucent_midnight_blue)));
			} else {
				v.setBackgroundColor(Color.parseColor(mContext.getString(R.color.list_item_translucent_black)));
				((TextView)v.findViewById(R.id.type_name)).setTextColor(Color.parseColor(mContext.getString(R.color.list_item_default_white)));
				ll.setBackgroundColor(Color.parseColor(mContext.getString(R.color.list_item_translucent_black)));
			}
			
			ExpenseType expenseTypes = mItems.get(position);
			
			if (expenseTypes != null) {
				TextView tv = (TextView) v.findViewById(R.id.type_name);
				TextView id = (TextView) v.findViewById(R.id.type_id);
				
 				if (tv != null) {
					tv.setText(expenseTypes.NAME);   
				}
 				
				if(id != null){
					id.setText(Long.toString(expenseTypes._ID));
				}
			}
			
			return v;
		}
	}
}